package parser;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.htmlparser.jericho.Attribute;
import net.htmlparser.jericho.Attributes;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.StartTag;
import net.htmlparser.jericho.nodoc.SequentialListSegment;

public class ElementoCompuesto extends Elemento {
	
	private  Sad parentSad;
	
	
	
	List<Elemento> elementosSAd = new ArrayList<Elemento>();
	
	public ElementoCompuesto(Elemento element) {
		
		elementosSAd.add(element);
	}
	
	public ElementoCompuesto() {
		
		
	}
	
	public ElementoCompuesto(Sad sad) {
		this.parentSad= sad;
	}

	public void add(Elemento elem){
		
		elementosSAd.add(elem);
	}
	
	public Elemento generate(Iterator<Element> elementos,Element elemento){
		
	
		if(elemento !=null ){
			Elemento elementSad = null;
	
			//Elemento simple
			if(elemento.isEmpty()){
									
//				 Attributes att= elemento.getAttributes();
//				 if(att!=null){
//				 Iterator<Attribute> atributos = att.iterator();
//				 while (atributos.hasNext()) {
//					 Attribute atributo = (Attribute) atributos.next();
//					atributo.toString();
//				}
//				 }	
				
				
				
				elementSad = new ElementoSimple(elemento);
				elementosSAd.add(elementSad.generate(elementos, elemento));
			}else{
				
				if(elemento.getChildElements().size()==0){
					
					//El primer h1 es el titulo
					if(!parentSad.isTittleSet()){
						if(esTitulo(elemento)){
							parentSad.setTitle(elemento.getContent().toString());
							System.out.println("Titulo: "+elemento.getContent());
							System.out.println(elemento.getDebugInfo());
						}
					}else{
						
							
//							System.out.println(elemento.getDebugInfo());
							
							processDocumentRoadMap(parentSad,elemento);
						
					}
					elementSad = new ElementoSimple(elemento);
					elementosSAd.add(elementSad.generate(elementos, elemento));
					
				}else{
				
				//Elemento compuesto
					StartTag tag = elemento.getStartTag();
					
					Iterator<Element> subElement = elemento.getChildElements().iterator();
					elementSad = new ElementoCompuesto(parentSad);
					elementosSAd.add(elementSad.generate(subElement,subElement.next()));
				
			 }
			}
			if(elementos.hasNext())
				generate(elementos, elementos.next());
		}
		return this;
	}

	private void processDocumentRoadMap(Sad sad, Element elemento) {
		
		if(!sad.isDocumentoRoadMapGenerated()){
			
			if(typeA(elemento)){
				
				
				if(getAttValue(elemento,"title").contains("DocumentRoadmap")){
					
					DocumentRoadMap doc = new DocumentRoadMap(parentSad);
					parentSad.setDocumentRoadMap(doc);
					doc.parse(parentSad.getUrlBase()+getAttValue(elemento, "href"));
				}
				
			}
			
		}
		
		
	}
	
	private void processArchitectureBackground(Sad sad, Element elemento) {
		
		if(!sad.isArchitectureBackgroundGenerated()){
			
			if(typeA(elemento)){
				
				
				if(getAttValue(elemento,"title").contains("DocumentRoadmap")){
					
					DocumentRoadMap doc = new DocumentRoadMap(parentSad);
					parentSad.setDocumentRoadMap(doc);
					doc.parse(parentSad.getUrlBase()+getAttValue(elemento, "href"));
				}
				
			}
			
		}
		
		
	}

	private String getAttValue(Element elemento,
			String key) {
		
		String rta="";
		Attributes att= elemento.getAttributes();
		boolean exist=false;
		 if(att!=null){
			 
		 Iterator<Attribute> atributos = att.iterator();
		 while (atributos.hasNext() && !exist) {
		 Attribute atributo = (Attribute) atributos.next();
		   if(atributo.getKey().equals(key))
			  exist= true;
		      rta= atributo.getValue();
		   }
		 }	
		return rta;
	}

	private boolean typeA(Element elemento) {
		
		return elemento.getName().equals(HTMLElementName.A);
	}

	private boolean isLink(Element elemento) {
		
		boolean typeA= typeA(elemento);
			
		boolean hrefExist= !"".equals(getAttValue(elemento, "href"));
		return(typeA && hrefExist);
	}

	private boolean esTitulo(Element elemento) {
	
		
		return(elemento.getName().equals(HTMLElementName.H1));
	}

}
